package com.pcj.blog.web.admin;/**
 * @param
 * @author pcj
 * @date 2021/12/27 14:21
 * @return
 */

import com.pcj.blog.po.User;
import com.pcj.blog.service.UserServiceImpl;
import com.pcj.blog.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

/**
 *
 * @className LoginController
 * @author pengcj
 * @date 2021/12/27 14:21
 */
@Controller
@RequestMapping("/admin")
public class LoginController {
    @Autowired
    private UserServiceImpl userService;
    @GetMapping
    public String LoginPage(){
        return "admin/login";
    }
    @PostMapping("/login")
    public String login(@RequestParam String username, @RequestParam String password, HttpSession session, RedirectAttributes redirectAttributes){
        User user=userService.checkuser(username, MD5Util.code(password));
        if(user!=null){
            user.setPassword(null);
            session.setAttribute("user",user);
            return "admin/index";
        }
        redirectAttributes.addFlashAttribute("message","用户名和密码错误");
        return "redirect:/admin";
    }
    @GetMapping("/loginout")
    public String LoginOut(HttpSession session){
        session.removeAttribute("user");
        return "redirect:/admin";
    }
}
